#include "os_sys.h"
#include "key_driver.h"
#include "key_bsp.h"

struct key_task_s
{
    SysTick_t time;

    u32 test3_cnt[11];
    u32 test4_cnt[11];
    u32 test6_cnt[11];

    u8 key_test;
    E_KEY key0;
    E_KEY key1;

    u32 test1;
    int test4;
	unsigned char test6;
    float test7;
};

struct key_task_s key_task={0};


static void key_test3_process(u32 key,E_KEY_TYPE type)
{
    key_task.test3_cnt[type]++;
    switch(key)
    {
        case KEY_PRESS_SINGLE_CLICK:
        break;
        case KEY_PRESS_DOUBLE_CLICK:
        break;
        case KEY_PRESS_REPEAT_CLICK:
        break;
        case KEY_PRESS_SHORT_START:
        break;
        case KEY_PRESS_SHORT_UP:
        break;
        case KEY_PRESS_LONG_START:
        break;
        case KEY_PRESS_LONG_UP:
        break;
        case KEY_PRESS_LONG_HOLD:
        break;
        case KEY_PRESS_LONG_HOLD_UP:
        break;
        default:break;
    }
}
static void key_test4_process(u32 key,E_KEY_TYPE type)
{
    key_task.test4_cnt[type]++;
}
static void key_test6_process(u32 key,E_KEY_TYPE type)
{
    key_task.test6_cnt[type]++;
}
static void key_event_process(u32 key,E_KEY_TYPE type)
{
    switch(key)
    {
        case KEY_TEST2:
            key_test6_process(key,type);
        break;
        case KEY_TEST3:
            key_test3_process(key,type);
        break;
        case KEY_TEST4:
            key_test4_process(key,type);
        break;
        default:break;
    }
}


static void key_main_run(void)
{
    if(0 == systick_get_difference_assign(&key_task.time,Tnms(100)))
		return ;

    switch (key_task.key_test)
    {
        case 0x01:
            key_bsp_function_change(key_task.key0,key_task.key1);
        break;
        case 0x10:
            msg_send(DATID_STATUS_LED_ON_OFF,0, TASKID_SWITCH);
        break;
        case 0x11:
            msg_send(DATID_STATUS_LED_ON_OFF,1, TASKID_SWITCH);
        break;
        case 0x12:
            msg_send(DATID_STATUS_LED_BLINK,MSG(200,400), TASKID_SWITCH);
        break;
        case 0x13:
            msg_send(DATID_EEPROM_TEST1_4_6_7_WRITE,MSG(&key_task.test1,&key_task.test4,&key_task.test6,&key_task.test7), TASKID_EEPROM,TASKID_SWITCH);
        break;
        case 0x14:
            msg_recv(DATID_EEPROM_TEST1_4_6_7_READ,MSG(&key_task.test1,&key_task.test4,&key_task.test6,&key_task.test7),TASKID_EEPROM);
        break;
        case 0x15:
            msg_send(DATID_EEPROM_SAVE,0, TASKID_EEPROM);
        break;

        default:
            break;
    }
    key_task.key_test=0;


}
static void key_peripheral_sleep(void)
{
    key_bsp_uninit();
}
static void key_message_process(E_DATAID DatID, const u32 data)
{
	switch(DatID)
	{
		default:break;
	}
}
static void key_variable_init(void)
{
    key_bsp_callback_set(key_event_process);
    key_bsp_init();
}
//CONSTRUCTOR_DEFAUTL()
void key_task_create(void)
{
	struct task_s task = TASK_DEFAULT_INIT();

	task.ID 		= TASKID_KEY;
	task.state		= TASK_STAT_FREE;
	task.TaskTime	= 0;
	task.run 		= key_main_run;
	task.sleep_init = key_peripheral_sleep;
	task.msg_process= key_message_process;
	task.task_tick 	= NULL;
	task.is_used 	= 1;
	task.task_init =   key_variable_init;
	sys_task_add(&task);
}
